package com.up.api.service;

import com.up.mongo.config.MongoTransactionName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.concurrent.CompletableFuture;


/**
 * @author: liuchengjun
 * @title: MongoService
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2023/12/14/014 17:31 (星期四)
 * @since V2.0
 */
@Component
public class MongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TransactionTemplate transactionTemplate;

    public void mongoAsyc(){
        //异步操作
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() ->{
            transactionTemplate.execute(status -> {
                try {
                    Query query1 = new Query();
                    query1.addCriteria(new Criteria("_id").is(1693822001046814720L));
                    Update update1 = new Update();
                    update1.set("desc", "我喜欢13");
                    mongoTemplate.updateFirst(query1, update1, "wf_classify");
                } catch (Exception e) {
                    status.setRollbackOnly();
                    throw new RuntimeException(e);
                }
                return null;
            });
        });
        CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() ->{
            transactionTemplate.execute(status -> {
                try {
                    Query query1 = new Query();
                    query1.addCriteria(new Criteria("_id").is(1693869382647873536L));
                    Update update1 = new Update();
                    update1.set("desc", "我喜欢13");
                    mongoTemplate.updateFirst(query1, update1, "wf_classify");
                    System.out.println("2");
                } catch (Exception e) {
                    status.setRollbackOnly();
                    throw new RuntimeException(e);
                }
                return null;
            });
        });
        //同步操作
        transactionTemplate.execute(status -> {
            try {
                Query query = new Query();
                query.addCriteria(new Criteria("_id").is(1668925978400788480L));
                Update update = new Update();
                update.set("name","我喜欢13");
                mongoTemplate.updateFirst(query,update,"wf_classify");
                int i = 10/0;
            } catch (Exception e) {
                status.setRollbackOnly();
                throw new RuntimeException(e);
            }
            return null;
        });
        CompletableFuture.allOf(completableFuture,completableFuture1).join();
    }
}
